package com.atguigu.sparkstreaming.utils

import java.text.SimpleDateFormat
import java.time.{Instant, LocalDateTime, ZoneId}
import java.time.format.DateTimeFormatter
import java.util.Date

/**
 * Created by Smexy on 2022/7/19
 *
 *
 */
object DateParseUtil {

  val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
  val dateTimeFormatter2: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
  val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
  val hourFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("HH")

  def parseMillTsToDateTime2(milliTs:Long):String={

    val dateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliTs), ZoneId.of("Asia/Shanghai"))

    dateTime.format(dateTimeFormatter2)

  }

  def parseMillTsToDateTime(milliTs:Long):String={

    val dateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliTs), ZoneId.of("Asia/Shanghai"))

    dateTime.format(dateTimeFormatter)

  }

  def parseMillTsToDate(milliTs:Long):String={

    val dateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliTs), ZoneId.of("Asia/Shanghai"))

    dateTime.format(dateFormatter)

  }

  def parseDateTimeStrToDate(dateTimeStr:String):String={

    val dateTime: LocalDateTime = LocalDateTime.parse(dateTimeStr, dateTimeFormatter)

    dateTime.format(dateFormatter)

  }

  def parseDateTimeStrToHour(dateTimeStr:String):String={

    val dateTime: LocalDateTime = LocalDateTime.parse(dateTimeStr, dateTimeFormatter)

    dateTime.format(hourFormatter)

  }



  def main(args: Array[String]): Unit = {

    val ts = 1658106590000l

    //Date对象
    val date = new Date(ts)

    //日期格式
    val simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    //使用日期格式取格式化Date
    val dateStr: String = simpleDateFormat.format(date)

    println(dateStr)


    println("java8之前--------------------")
    println("java8之后--------------------java.time--------静态方法")

    //日期格式
    val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

    //LocateDate：日期  LocateDateTime: 日期时间
    val dateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.of("Asia/Shanghai"))

    //使用日期格式取格式化Date
    val dateTimeStr: String = dateTime.format(formatter)

    println(dateTimeStr)


    println("java8之后时间的转换--------------------")

    val datetimestr ="2022-07-19 13:20:34"

    val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
    val formatter2: DateTimeFormatter = DateTimeFormatter.ofPattern("HH")

    val dateTime1: LocalDateTime = LocalDateTime.parse(datetimestr, formatter)

    println(dateTime1.format(formatter1))
    println(dateTime1.format(formatter2))






  }

}
